package com.sunlei.webtest.quickstart.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * @Author: sun
 * @Description:
 * @Data: create in 16:39 2018/11/23
 * @Modfied by:
 */
public class MyConsumer extends DefaultConsumer {

    private Channel channel;

    /**
     * Constructs a new instance and records its association to the passed-in channel.
     *
     * @param channel the channel to which this consumer is attached
     */
    public MyConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

    /**
     * @param consumerTag 消费者对象标签
     */
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//        super.handleDelivery(consumerTag, envelope, properties, body);
        System.out.println("[myconsumer]:" + consumerTag + " " + envelope.getExchange());
        //手动ack消息
        channel.basicAck(envelope.getDeliveryTag(),false);
    }
}
